package com.hwpt.hwaccount.feature.auth.ui;

import android.graphics.Color;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;

import com.hwpt.hwaccount.R;
import com.hwpt.hwaccount.core.data.remote.RetrofitClient;
import com.hwpt.hwaccount.core.data.remote.api.ApiService;
import com.hwpt.hwaccount.feature.auth.RepwdContract;
import com.hwpt.hwaccount.feature.auth.RepwdModel;
import com.hwpt.hwaccount.feature.auth.RepwdPresenter;

public class RepwdActivity extends AppCompatActivity implements RepwdContract.View {

    private EditText etPhone, etVerificationCode, etNewPwd, etConfirmPwd;
    private Button btnGetCode, btnNext, btnLogin;
    private LinearLayout layoutResetPwd;
    private TextView tvTitle;
    private ImageButton btnBack;

    private RepwdContract.Presenter presenter;

    // 在RepwdActivity的onCreate方法中修改
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_repwd);

        // 初始化ApiService（根据您的项目实际情况获取）
        ApiService apiService = RetrofitClient.getApiService(this);

        // 初始化Presenter和Model
        RepwdModel model = new RepwdModel(apiService);
        presenter = new RepwdPresenter(this, model);

        initViews();
        setupListeners();
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (presenter != null) {
            presenter.onDestroy();
        }
    }

    private void initViews() {
        etPhone = findViewById(R.id.et_username);
        etVerificationCode = findViewById(R.id.et_verification_code);
        etNewPwd = findViewById(R.id.et_new_pwd);
        etConfirmPwd = findViewById(R.id.et_confirm_pwd);

        btnGetCode = findViewById(R.id.btn_get_code);
        btnNext = findViewById(R.id.btn_next);
        btnLogin = findViewById(R.id.btn_login);
        btnBack = findViewById(R.id.btn_back);

        layoutResetPwd = findViewById(R.id.layout_reset_pwd);
        tvTitle = findViewById(R.id.tv_title);
    }

    private void setupListeners() {
        // 返回按钮点击事件
        btnBack.setOnClickListener(v -> finish());

        // 获取验证码按钮点击事件
        btnGetCode.setOnClickListener(v -> {
            String phone = etPhone.getText().toString().trim();
            presenter.sendVerificationCode(phone);
        });

        // 下一步按钮点击事件
        btnNext.setOnClickListener(v -> {
            String phone = etPhone.getText().toString().trim();
            String code = etVerificationCode.getText().toString().trim();
            presenter.verifyCode(phone, code);
        });

        btnLogin.setOnClickListener(v -> {
            String newPwd = etNewPwd.getText().toString().trim();
            String confirmPwd = etConfirmPwd.getText().toString().trim();

            if (TextUtils.isEmpty(newPwd)) {
                showToast("请输入新密码");
                return;
            }

            if (TextUtils.isEmpty(confirmPwd)) {
                showToast("请再次输入新密码");
                return;
            }

            if (!newPwd.equals(confirmPwd)) {
                showToast("两次输入的密码不一致");
                return;
            }

            presenter.resetPassword(newPwd);
        });
    }

    @Override
    public void showToast(String message) {
        runOnUiThread(() -> Toast.makeText(this, message, Toast.LENGTH_SHORT).show());
    }

    @Override
    public void startCountDownTimer() {
        runOnUiThread(() -> {
            new CountDownTimer(60000, 1000) {
                @Override
                public void onTick(long millisUntilFinished) {
                    btnGetCode.setEnabled(false);
                    btnGetCode.setTextColor(Color.GRAY);
                    btnGetCode.setText("重新发送(" + millisUntilFinished / 1000 + "s)");
                }

                @Override
                public void onFinish() {
                    btnGetCode.setEnabled(true);
                    btnGetCode.setTextColor(ContextCompat.getColor(RepwdActivity.this, android.R.color.holo_blue_light));
                    btnGetCode.setText("获取验证码");
                }
            }.start();
        });
    }

    @Override
    public void switchToResetPassword() {
        runOnUiThread(() -> {
            // 隐藏验证部分
            etPhone.setVisibility(View.GONE);
            etVerificationCode.setVisibility(View.GONE);
            btnGetCode.setVisibility(View.GONE);
            btnNext.setVisibility(View.GONE);

            // 更新标题
            tvTitle.setText("汉王账号 修改密码");

            // 显示重置密码部分
            layoutResetPwd.setVisibility(View.VISIBLE);
        });
    }

    @Override
    public void finishActivity() {
        runOnUiThread(() -> finish());
    }
}